class Solution {
public:
    void dfs(TreeNode* root, int k, vector<vector<int>> &ans){
        if(root == NULL) return;
        if(k == ans.size()) ans.emplace_back(vector<int>());
        ans[k].emplace_back(root->val);
        dfs(root->left, k + 1, ans);
        dfs(root->right, k + 1, ans);
        return;
    }
    
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> ans;
        dfs(root, 0, ans);
        for(int i = 0, j = ans.size() - 1; i < j ; i++, j--){
            swap(ans[i], ans[j]);
        }
        return ans;
    }
};